package com.aptemo.webetek.client.widgets.players;

import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EnumUserRole;
import com.aptemo.webetek.client.enums.EnumUserStatus;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.widgets.FocusTextBox;
import com.aptemo.webetek.client.widgets.dialogs.DecisionDialog;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class PlayerResume
		extends Composite implements PlayerResumePresenter.Display
{

	private static PlayersUiBinder	uiBinder	= GWT.create(PlayersUiBinder.class);

	interface PlayersUiBinder extends UiBinder<Widget, PlayerResume>
	{
	}

	@UiField
	Label					infoField;

	@UiField
	Label					titleField;

	@UiField
	Label					emailField;

	@UiField
	ListBox					statusField;

	@UiField
	ListBox					roleField;

	@UiField
	FocusTextBox			balanceField;

	@UiField
	Button					deleteBtn;

	@UiField
	Button					saveBtn;

	@UiField
	Button					newPaymentBtn;

	@UiField
	TextBox					paymentAmountField;

	private DecisionDialog	deleteConfirmation	= new DecisionDialog("Deleting player...");

	public PlayerResume()
	{
		initWidget(uiBinder.createAndBindUi(this));

		statusField.addItem("None", "");
		EnumUserStatus[] statuses = EnumUserStatus.values();
		for (EnumUserStatus item : statuses)
		{
			statusField.addItem(item.name(), item.toString());
		}

		roleField.addItem("None", "");
		EnumUserRole[] roles = EnumUserRole.values();
		for (EnumUserRole item : roles)
		{
			roleField.addItem(item.name(), item.toString());
		}

	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasText getBalanceField()
	{
		return balanceField;
	}

	@Override
	public HasText getEmailField()
	{
		return emailField;
	}

	@Override
	public void setRoleField(EnumUserRole role)
	{
		String rl = role.toString();
		for (int i = 0; i < roleField.getItemCount(); i++)
		{
			String item = roleField.getValue(i);
			if (rl.equals(item))
			{
				roleField.setSelectedIndex(i);
				return;
			}
		}
	}

	@Override
	public void setStatusField(EnumUserStatus status)
	{
		String st = status.toString();
		for (int i = 0; i < statusField.getItemCount(); i++)
		{
			String item = statusField.getValue(i);
			if (st.equals(item))
			{
				statusField.setSelectedIndex(i);
				return;
			}
		}
	}

	@Override
	public HasText getTitleField()
	{
		return titleField;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public HasClickHandlers getSaveButton()
	{
		return saveBtn;
	}

	@Override
	public EnumUserRole getSelectedRole()
	{
		int index = roleField.getSelectedIndex();
		String role = roleField.getValue(index);
		return EnumUserRole.valueOf(role);
	}

	@Override
	public EnumUserStatus getSelectedStatus()
	{
		int index = statusField.getSelectedIndex();
		String status = statusField.getValue(index);
		return EnumUserStatus.valueOf(status);
	}

	@Override
	public HasClickHandlers getNewPaymentButton()
	{
		return newPaymentBtn;
	}

	@Override
	public HasValue<String> getPaymentAmountField()
	{
		return paymentAmountField;
	}

	@UiHandler("deleteBtn")
	void deleteHandler(ClickEvent event)
	{
		deleteConfirmation.center();
	}

	@Override
	public HasDecisionHandlers getDeleteConfirmation()
	{
		return deleteConfirmation;
	}

	@Override
	public void hideDeleteDialog()
	{
		deleteConfirmation.hide();
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

}
